# Time-stamp: <27 Apr 00 18:22:56 giron1>

=head1 NAME

 GLOBALS - global variables package

=head1 DESCRIPTION

This package manipulates globals variables defined in I<globals.pl> files. 

=head1 SYNOPSIS

 my $glob = new GLOBALS;

 $glob->{GLOBALS_PATH} = "$ENV{HOME}/db/";
 $glob->{DEF_GLOBALS} =  $glob->{GLOBALS_PATH} . 'deu/globals.pl';
 $glob->requireglob;
 my $corp = 'all';
 $glob->printglob($corp);
 $glob->undefglob;

=head1 AUTHOR

 $Id: GLOBALS.pm,v 1.1 2007/10/08 09:26:03 schweitz Exp $
 Franck Giron

=head1 LOG

 $Log: GLOBALS.pm,v $
 Revision 1.1  2007/10/08 09:26:03  schweitz
 version 2.4, lib vergessen

 Revision 1.9  2000/04/27 16:23:55  giron1
 Pod documentation updated


=cut

package GLOBALS;

#----------------------------------------------------------------------

=head1 FUNCTIONS

here is a description of the available functions

=cut

#----------------------------------------------------------------------

=head2 new

constructor

=cut

sub new{
  my $that = shift;
  my $class = ref($that) || $that;
  my $self = {};
  bless $self, $class;
  return $self;
}

#----------------------------------------------------------------------

=head2 requireglob

reads a global file description

=cut

sub requireglob{
  my $that = shift; 

  # Undefined previous global variables 
  # if CORPUS_H was previously defined

  if (defined($GLOBALS::CORPUS_H)) {$that->undefglob;}

  # Reads the file if readable
  my $defg = $that->{DEF_GLOBALS};
  if (-r $defg) {
    $GLOBALS_PATH =  $that->{GLOBALS_PATH};
    require $defg or die "Error by requiring $defg.";
  }
  # or search an environment variable definition
  else {
    warn "File $defg not existent. Check your global root...\n";
    if ( exists($ENV{DB_GLOBALS}) ) {
      if (-r "$ENV{DB_GLOBALS}.pl") {
	warn "using $ENV{DB_GLOBALS}.pl\n";
	require "$ENV{DB_GLOBALS}.pl";
      }
      else {
	die "Environment file \$DB_GLOBALS $ENV{DB_GLOBALS}.pl not existent\n";
      }
    } else {
      warn "Environment variable \$DB_GLOBALS also not defined.\n";
      die "A globals.pl has to be defined ...\n";
    }
  }
}

=head2 undefglob

undefines all GLOBALS variables

=cut

sub undefglob{
  my $that = shift;

  foreach (keys %GLOBALS::) {
    local  *sym = $GLOBALS::{$_};
    undef $sym if (defined $sym);
    undef @sym if (defined @sym);
    undef %sym if (defined %sym);
  }
}

=head2 printglob ($corp)

prints I<all> GLOBALS variables if C<$corp = 'all'> or from a
particular corpus I<$corp> by matching this string with the names of
the variables

=cut

sub printglob{
  my $that = shift;
  my ($corp) = @_;

  foreach (sort keys %GLOBALS::) {
    local *sym = $GLOBALS::{$_};

    if (/$corp/i or $corp eq 'all') {
      if (defined $sym) {print "\$$_ = $sym\n";}
      if (defined @sym) {print "\@$_ = (", join(' ',@sym), ")\n";}
      if (defined %sym) {
	print "\%$_ is defined:\n";
	my $k;
	foreach $k (sort keys %sym) {
	  print "$k:",join(' ', @{$sym{$k}}), "\n";
	}
      }
    }
  }
}

sub DESTROY {
  my $that = shift;
}

1;
